package com.example.dqdq.lzyim_asm_tvapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;

import java.util.Timer;
import java.util.TimerTask;

public class GroupScrollView extends HorizontalScrollView implements Runnable {
    private View inner;
    private Bitmap bitmap = null;
    private int x;
    private int width;
    private int pWidth;
    private int pHeight;
    private Timer textTimer;

    /**
     * 滚动步长
     */
    private int step = 1;
    /**
     * 滚动间隔距离
     */
    private int space = 100;
    private int delay = 500;


    public GroupScrollView(Context context) {
        super(context);
//        setBackgroundColor(android.R.color.transparent);


    }
    public GroupScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        setBackgroundColor(android.R.color.transparent);
    }

    @Override
    protected void onFinishInflate() {
        if (getChildCount() == 1) {
            inner = getChildAt(0);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getWidth() == 0) {
            android.view.ViewGroup.LayoutParams lp = getLayoutParams();
            lp.width = pWidth;
            lp.height = pHeight;
            setLayoutParams(lp);
        }
        if (bitmap == null && inner != null) {
            width = inner.getMeasuredWidth();
            bitmap = Bitmap.createBitmap(width, inner.getHeight(),Config.RGB_565);
            Canvas canvas1 = new Canvas(bitmap);
            inner.draw(canvas1);
            pWidth = getWidth();
            pHeight = getHeight();
            if (inner != null) {
                removeViewInLayout(inner);
                inner = null;
            }
//            run();
            textTimer = new Timer();//定时自动关闭字幕
//            textrecLen = 60;
            textTimer.schedule(textTimerTask, 1000, 100);
        }
        if (bitmap != null) {
            int nowX = x;
            nowX -= step;
            canvas.drawBitmap(bitmap, nowX, 0, null);
            if (nowX < 0) {
                canvas.drawBitmap(bitmap, width + nowX + space, 0, null);
            }
            if (nowX <= -width) {
                nowX = 0;
            }
            x = nowX;
        }
        super.onDraw(canvas);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String msgStr=(String) msg.obj;
            if (msgStr.equals("reSet"))
            invalidate();

            super.handleMessage(msg);
        }
    };


    /**
     * 定时关闭字幕
     */
    TimerTask textTimerTask = new TimerTask() {
        @Override
        public void run() {
            Message msg=new Message();
            msg.obj="reSet";
            handler.sendMessage(msg);
            handler.postDelayed(this, delay);

        }
    };
    @Override
    public void run() {
//        invalidate();
//        handler.postDelayed(this, delay);
    }

}
